﻿
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;
using Lrd.Template.Core.Infrastructure.Extensions;

namespace Lrd.Template.Core.DynamicWebApi
{
    /// <summary>
    /// 动态API配置
    /// </summary>
   public class DynamicWebApiOptions
    {
        public DynamicWebApiOptions()
        {
            RemoveControllerPostfixes = new List<string>() { "AppService", "ApplicationService" };
            RemoveActionPostfixes = new List<string>() { "Async" };
            FormBodyBindingIgnoredTypes = new List<Type>() { typeof(IFormFile) };
        }


        /// <summary>
        /// API HTTP Verb.
        /// <para></para>
        /// Default value is "POST".
        /// </summary>
        public string DefaultHttpVerb { get; set; } = "POST";

        /// <summary>
        /// 默认区域名
        /// </summary>
        public string DefaultAreaName { get; set; }

        /// <summary>
        ///所有API的路由前缀
        /// <para></para>
        /// 默认值为api
        /// </summary>
        public string DefaultApiPrefix { get; set; } = "api";

        /// <summary>
        /// 删除动态API类（控制器）名称后缀.
        /// <para></para>
        /// 默认值为{"AppService", "ApplicationService"}.
        /// </summary>
        public List<string> RemoveControllerPostfixes { get; set; }

        /// <summary>
        /// 删除动态API类的方法（操作）后缀。
        /// <para></para>
        /// 默认值为 {"Async"}.
        /// </summary>
        public List<string> RemoveActionPostfixes { get; set; }

        /// <summary>
        /// 忽略MVC窗体绑定类型。
        /// </summary>
        public List<Type> FormBodyBindingIgnoredTypes { get; set; }

        /// <summary>
        /// 验证所有配置是否有效
        /// </summary>
        public void Valid()
        {
            if (DefaultHttpVerb.IsNullOrEmpty())
            {
                throw new ArgumentException($"{nameof(DefaultHttpVerb)} 不能为空。");
            }

            if (DefaultAreaName.IsNullOrEmpty())
            {
                DefaultAreaName = string.Empty;
            }

            if (DefaultApiPrefix.IsNullOrEmpty())
            {
                DefaultApiPrefix = string.Empty;
            }

            if (FormBodyBindingIgnoredTypes is null)
            {
                throw new ArgumentException($"{nameof(FormBodyBindingIgnoredTypes)} 不能为空指针.");
            }

            if (RemoveControllerPostfixes == null)
            {
                throw new ArgumentException($"{nameof(RemoveControllerPostfixes)} 不能为空指针.");
            }
        }
    }
}
